from django import forms
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.template import RequestContext

from wanbooks.sharing.models import add_items
from wanbooks.catalog.importers import parse_gbs_xml

def main_page(request):
    return render_to_response('index.html',{},
                              context_instance=RequestContext(request))

class LoadGoogleBooksXml(forms.Form):
    xml = forms.FileField(label=u'Please locate the "MyLibrary.xml" file')
    
@login_required()
def upload_gbs_xml(request):
    u'''Post file from Google Book Search -> My Library -> Export my library'''
    if request.method == 'POST':
        form = LoadGoogleBooksXml(request.POST, request.FILES)
        if form.is_valid():
            vars = parse_gbs_xml(request.FILES['xml'])
            vars['new_editions'], vars['new_creations'] = add_items(
                vars['contents'], request.user)
            return render_to_response('confirm_load.html', vars)
    else:
        form = LoadGoogleBooksXml()
    return render_to_response('upload_gbs_xml.html', {'form': form})
